﻿function DDL_Category_changed(w_) {
    var w = $(w_);
    DDL_Category_SetVal(w);
    if (w.val() == 0) return;
    $.ajax({
        type: "post", dataType: "jsonp",
        url: "http://api.aiyoubb.com/category.ashx",
        data: { FatherCategoryID: w.val() },
        success: function (callBak) {
            if (callBak.IsSuccess) DDL_Category_changed_ok(w, callBak.ReturnVal);
            else if (callBak.ErrorString != "") alert(callBak.ErrorString);
        }
    });

}
function DDL_Category_changed_ok(w, ListCategory_) {
    var thisCategoryLev = w.attr("data-categorylev");
    var sonSelect = w.siblings("select[data-categorylev=" + accAdd(thisCategoryLev, 1) + "]");
    sonSelect.find("option").remove();
    var NeedLeaderWork = w.parent().find(":checkbox").is(":checked");
    if (NeedLeaderWork) sonSelect.append("<option value=\"0\">全部</option>");
    for (var i = 0; i < ListCategory_.length; i++) sonSelect.append("<option value=\"" + ListCategory_[i].id + "\">" + ListCategory_[i].Name + "</option>");
    if (NeedLeaderWork) sonSelect.val(0);
    else {
        sonSelect.val(ListCategory_[0].id);

    }
    if (thisCategoryLev * 1 == 1) DDL_Category_changed(sonSelect);
    //else DDL_Category_SetVal(sonSelect);
}
function DDL_Category_SetVal(w) {
    var _val = w.val() * 1;
    if (_val == 0) {
        var thisCategoryLev = w.attr("data-categorylev") * 1;
        if (thisCategoryLev == 1) {
            w.siblings("select[data-categorylev=2]").find("option").remove();
            w.siblings("select[data-categorylev=3]").find("option").remove();
        } else {
            _val = w.siblings("select[data-categorylev=" + (thisCategoryLev - 1) + "]").val();
            if (thisCategoryLev == 2) w.siblings("select[data-categorylev=3]").find("option").remove();
        }
    }
    w.siblings(":text").val(_val);
}